package core.proto.rightwindow;

import static app.constants.CollabVizConstants.ENCODING;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;

import com.oreilly.servlet.HttpMessage;

import core.proto.InfoViz;
import core.proto.Util;
import core.proto.annotation.AnnotationUI;

public class WebUtil implements MessageTypes {

	public final static String SERVER_ABS_URL = "http://" + InfoViz.codebase().getAuthority() + "/ChatServlet/chatservlet"; 

	public static void sendChatMessage (String message) throws IOException {
		URL url = new URL(SERVER_ABS_URL);
		
		HttpMessage msg = new HttpMessage(url); 
		
		Properties props = new Properties(); 
		props.put(CONTENT, message);
		props.put(MSG_TYPE_KEY, CHAT_TYPE);
		props.put(ACTION_TYPE, CHAT_PUSH);
		
		msg.sendPostMessage(props); // Post message for chat.
	}

	public static InputStream pullChatMessage () throws IOException {
		URL url;
		url = new URL(SERVER_ABS_URL);
		HttpMessage msg = new HttpMessage(url);
		Properties props = new Properties(); 
		props.put(MSG_TYPE_KEY, CHAT_TYPE);
		props.put(ACTION_TYPE, CHAT_PULL);
		return msg.sendPostMessage(props);
	}
	
	public static void sendHighlightNotification (String message) throws IOException {
		URL url = new URL(SERVER_ABS_URL);
		
		HttpMessage msg = new HttpMessage(url); 
		
		Properties props = new Properties(); 
		

		props.put(CONTENT, message);
		props.put(MSG_TYPE_KEY, NOTIFICATION_TYPE);
		props.put(ACTION_TYPE, NOTIFICATION_PUSH);
		
		msg.sendPostMessage(props); 
	}
	
	public static InputStream pullHighlightNotification () throws IOException {
		URL url = new URL(SERVER_ABS_URL);
		
		HttpMessage msg = new HttpMessage(url); 
		
		Properties props = new Properties(); 
		props.put(MSG_TYPE_KEY, NOTIFICATION_TYPE);
		props.put(ACTION_TYPE, NOTIFICATION_PULL);
		
		return msg.sendPostMessage(props); 
	}
	
	public static void sendSystemNote(String note) throws IOException{
		URL url = new URL(SERVER_ABS_URL);
		
		HttpMessage msg = new HttpMessage(url); 
		
		Properties props = new Properties(); 
		

		props.put(CONTENT, note);
		props.put(MSG_TYPE_KEY, SYSTEM_TYPE);
		props.put(ACTION_TYPE, SYSTEM_PUSH);
		
		msg.sendPostMessage(props); 
	}
	
	public static InputStream pullSystemNote() throws IOException {
		URL url = new URL(SERVER_ABS_URL);
		
		HttpMessage msg = new HttpMessage(url); 
		
		Properties props = new Properties(); 
		props.put(MSG_TYPE_KEY, SYSTEM_TYPE);
		props.put(ACTION_TYPE, SYSTEM_PULL);
		
		return msg.sendPostMessage(props); 
	}
	
	public static void sendNewUser(String newUser) throws IOException{
		URL url = new URL(SERVER_ABS_URL);
		
		HttpMessage msg = new HttpMessage(url); 
		
		Properties props = new Properties(); 
		

		props.put(CONTENT, newUser);
		props.put(MSG_TYPE_KEY, NEWUSER_TYPE);
		props.put(ACTION_TYPE, NEWUSER_PUSH);
		
		msg.sendPostMessage(props); 
	}
	
	public static InputStream pullNewUser() throws IOException {
		URL url = new URL(SERVER_ABS_URL);
		
		HttpMessage msg = new HttpMessage(url); 
		
		Properties props = new Properties(); 
		props.put(MSG_TYPE_KEY, NEWUSER_TYPE);
		props.put(ACTION_TYPE, NEWUSER_PULL);
		
		return msg.sendPostMessage(props); 
	}
	
	public static void sendDeleteMessage(Long sessID) throws IOException{
		URL url = new URL(SERVER_ABS_URL);
		
		HttpMessage msg = new HttpMessage(url); 
		
		Properties props = new Properties(); 
		props.put(CONTENT, sessID + "");
		props.put(MSG_TYPE_KEY, "delete");
		props.put(ACTION_TYPE, CHAT_PUSH);
		
		msg.sendPostMessage(props); // Post message for chat.
	}
	
	public static InputStream pullDeleteMessage() throws IOException {
		URL url = new URL(SERVER_ABS_URL);
		
		HttpMessage msg = new HttpMessage(url); 
		
		Properties props = new Properties(); 
		props.put(MSG_TYPE_KEY, DELETE_TYPE);
		props.put(ACTION_TYPE, DELETE_PULL);
		
		return msg.sendPostMessage(props); 
	}
	
	

	public static void sendPostIt (String message) throws IOException {
		HttpMessage msg = new HttpMessage(new URL(SERVER_ABS_URL));
		
		boolean shouldShareThis = true;
		
		if(InfoViz.postitSharingDiscretion){
			if(!PostItNetworking.getSharingDiscretion(PostIt.toProperties(message)) &&
					PostIt.getOwnerToken(PostIt.toProperties(message)).equals(InfoViz.UserID)){
				shouldShareThis = false;
			}
		}
		if(InfoViz.sharePostits && shouldShareThis){
			Properties props = new Properties();
			props.put(MSG_TYPE_KEY, MessageTypes.ANNO_TYPE);
			props.put(ACTION_TYPE, MessageTypes.ANNO_PUSH);
			props.put(CONTENT, message);
		
			msg.sendPostMessage(props);
		}
	}
	
	public static void sendPostItDeleteBypass (String message) throws IOException {
		try {
			Properties properties = PostIt.toProperties(message);
			properties.setProperty("todelete", "true");
			
			ByteArrayOutputStream stream = new ByteArrayOutputStream();
			properties.store(stream, null);
			message =  stream.toString(ENCODING);
			
			HttpMessage msg = new HttpMessage(new URL(SERVER_ABS_URL));
			Properties props = new Properties();
			props.put(MSG_TYPE_KEY, MessageTypes.ANNO_TYPE);
			props.put(ACTION_TYPE, MessageTypes.ANNO_PUSH);
			props.put(CONTENT, message);
		
			msg.sendPostMessage(props);
			
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}

	public static InputStream pullPostItUpdates () throws IOException {
		HttpMessage msg = new HttpMessage(new URL(SERVER_ABS_URL));
		Properties props = new Properties();
		props.put(MSG_TYPE_KEY, MessageTypes.ANNO_TYPE);
		props.put(ACTION_TYPE, MessageTypes.ANNO_PULL);
		
		return msg.sendPostMessage(props);
	}
	
	public static void sendGroupConsist (String message) throws IOException {
		HttpMessage msg = new HttpMessage(new URL(SERVER_ABS_URL));
		
		Properties props = new Properties();
		props.put(MSG_TYPE_KEY, MessageTypes.GROUP_TYPE);
		props.put(ACTION_TYPE, MessageTypes.GROUP_PUSH);
		props.put(CONTENT, message);
		
		msg.sendPostMessage(props);
	}

	public static InputStream pullGroupConsist () throws IOException {
		HttpMessage msg = new HttpMessage(new URL(SERVER_ABS_URL));
		Properties props = new Properties();
		props.put(MSG_TYPE_KEY, MessageTypes.GROUP_TYPE);
		props.put(ACTION_TYPE, MessageTypes.GROUP_PULL);
		
		return msg.sendPostMessage(props);
	}
	
	public static InputStream getNextSessionId () throws IOException {
		HttpMessage msg = new HttpMessage(new URL(SERVER_ABS_URL));
		
		Properties props = new Properties();
		props.put(MSG_TYPE_KEY, MessageTypes.SESSION_ID);
		
		return msg.sendPostMessage(props);
	}

}
